﻿// ==UserScript== 
// @author 		   Iñaki Úcar
// @name           Colaboraciones Amazings
// @version 	   0.3
// @description    Muestra estadísticas sobre las colaboraciones de Amazings
// @namespace      Amazings.es
// @include        http://amazings.es/colaboradores/
// @require		   https://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js
// ==/UserScript==

/**
 * Sorting elements with jQuery
 * Author: James Padolsey
 * Web: http://james.padolsey.com/javascript/sorting-elements-with-jquery/
 */
jQuery.fn.sortElements = (function(){
    var sort = [].sort;
    return function(comparator, getSortable) {
        getSortable = getSortable || function(){return this;};
        var placements = this.map(function(){
            var sortElement = getSortable.call(this),
                parentNode = sortElement.parentNode,
                // Since the element itself will change position, we have
                // to have some way of storing its original position in
                // the DOM. The easiest way is to have a 'flag' node:
                nextSibling = parentNode.insertBefore(
                    document.createTextNode(''),
                    sortElement.nextSibling
                );
            return function() {
                if (parentNode === this) {
                    throw new Error(
                        "You can't sort elements if any one is a descendant of another."
                    );
                }
                // Insert before flag:
                parentNode.insertBefore(this, nextSibling);
                // Remove flag:
                parentNode.removeChild(nextSibling);
            };
        });
        return sort.call(this, comparator).each(function(i){
            placements[i].call(getSortable.call(this));
        });
    };
})();

var divs = $('#content > div[id^="author-"]');
var head = $('#content > div[id^="page-"]');
var text = head.children('div.entry-content');
head.children('div.entry-content').html(text.html().replace(/Más de 80/ig, divs.length));
var h1 = head.children('h1.entry-title');
h1.append(' | ');
$('<span id="asdfg">0</span>').appendTo(h1);
h1.append(' colaboraciones totales');
var h2 = $('h2.author-name');
h2.append(' | ');
$('<span class="asdfg">0</span>').appendTo(h2);
var description = $('div.author-description');
description.html('<ol></ol>');
description.children('ol').css('list-style-type', 'decimal').css('margin-left', '30px');

function getPosts() {
	var url = $(this).children('.author-meta').children('.vcard').children('a').attr('href');
	var h2 = $(this).children('h2.author-name');
	var description = $(this).children('div.author-description');
	function callback(data) {
		var urls = $(data).find('h2.entry-title').children('a');
		var posts = urls.length;
		description.children('ol').append(urls);
		description.children('ol').children('a').each(function(index) {
			$(this).wrap('<li />');
		});
		$('span#asdfg').html(parseInt($('span#asdfg').html()) + posts);
		h2.children('span.asdfg').html(parseInt(h2.children('span.asdfg').html()) + posts);
		$('span.asdfg').sortElements(function(a,b){ return parseInt($(a).text()) < parseInt($(b).text()) ? 1 : -1; }, function(){ return this.parentNode.parentNode; });
		var pager = $(data).find('.pager-next');
		if (pager.length > 0) {
			$.get(pager.attr('href'), callback);
		}
	}
	$.get(url, callback);
}

divs.each(getPosts);